package com.zcy.basic.util;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 自己码对象工具类
 */
public class ClassUtils {
    /**
     * 获取一个包下面的所有类全限定名
     * @param packageNames 包
     * @return
     */
//    public static List<Class> getAllClassName(String packageNames){
//        String[] split = packageNames.split(",");
//        List<Class> result = new ArrayList<>();
//        for (String packageName : split) {
//            // 包相对路径
//            String packagePath = packageName.replace(".","/"); //把包路径中的点替换为目录
//            // 资源URL
//            URL url = ClassLoader.getSystemResource(""); //获取classapth
//            File[] files = new File(url.getPath() + packagePath) //获取classapth+packagePath,就是完整的路径
//                    .listFiles(file -> file.getName().endsWith(".class"));
//            if(files == null || files.length < 1){
//                continue;
//            }
//            for(File file : files){
//                // 输出类名称
//                String fileName = file.getName();
//                fileName = fileName.substring(0,fileName.lastIndexOf("."));
//                //全限定类名
//                String allName = packageName+"."+fileName;
//                try {
//                    result.add(Class.forName(allName));
//                } catch (ClassNotFoundException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        return result;
//    }

    public static List<String> getAllClassName (String packageNames) {
        List<String> classNameList = new ArrayList<>();
        String[] fullPackageNameArray = packageNames.split(",");
//        String systemPath = ClassLoader.getSystemResource("").getPath();
        System.out.println("====================================");
        System.out.println(Arrays.toString(fullPackageNameArray));
        System.out.println(Objects.requireNonNull(ClassLoader.getSystemClassLoader().getResource("")).getPath());
        String systemPath = ClassLoader.getSystemResource("").getPath();
        System.out.println("====================================");
        System.out.println(systemPath);
        for (String packageName : fullPackageNameArray) {
            String packagePath = packageName.replace(".", "/");
            String packageAbsolutePath = systemPath + "/" + packagePath;
            File[] files = new File(packageAbsolutePath).listFiles(file -> file.getName().endsWith(".class"));
            if (Objects.nonNull(files)) {
                for (File file : files) {
                    String fileName = file.getName();
                    String className = packageName + "." + fileName.substring(0, fileName.indexOf("."));
                    classNameList.add(className);
                }
            }
        }
        return classNameList;
    }
}